/*
 *model the function of MS windows "search file" by using Java 
 */
package charles.testproject.uifilesearch;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author Sancho_lai
 *
 */
public class UIfileSearch extends JFrame {
	
	public UIfileSearch() {
		this.setTitle("文件搜索JAVA实现");
		this.setSize(600,400);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(new SearchFilePanel());
	}
	
	public static void main(String[] args) {
		UIfileSearch test = new UIfileSearch();
	System.out.println("5");
		test.setVisible(true);
	}
}

class SearchFilePanel extends JPanel{
	
	/**
	 * 路径浏览和查找按钮 
	 * Buttons for browsing the file system and for starting the search
	 */
	private JButton browse,search;
	/**
	 * 查找条件，路径，查找过程
	 * the conditions,directory,process of Searching,
	 */
	private JLabel filter, directory,statusShow1,statusShow2;
	/**
	 * 输入查找条件和路径的文本框
	 * TextField for inputing the search filter and the file path
	 */
	private JTextField textFilter,textDirectory;
	/**
	 * 显示查找结果的文本区
	 * the textArea to show the result of searching
	 */
	private JTextArea result;
	
	
	private File selectedFile;
	/**
	 * 找到的文件数量
	 * number of file had found
	 */
	private long countFiles;
	/**
	 * temporary variable to store the <filter> and <directory>
	 */
    private String p ,f;
	
	public  SearchFilePanel() {
		
		filter = new JLabel("All or part of File name",2);
		directory = new JLabel("Look in",2);
		statusShow1 = new JLabel("ready to search...",2);
		statusShow2 = new JLabel("The numbers of files had been found:");
		textFilter = new JTextField(15);
		textDirectory = new JTextField(15);

		result = new JTextArea();
		result.setEditable(false);
		JScrollPane js = new JScrollPane(result);
		
		browse = new JButton("Browse");
		search = new JButton("Start Search");
		/**
		 * 给浏览按钮设置监听事件
		 * add ActionListener for button <t>browse<\t> directly
		 */
		browse.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent evt) {
				JFileChooser jfc = new JFileChooser();
				//Just choose a directory address
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int d = jfc.showDialog(null, "Browse the files");
				if(d == JFileChooser.APPROVE_OPTION) {
					selectedFile = jfc.getSelectedFile();
					textDirectory.setText(selectedFile.getPath());
				}
			}
			
		});
	 	/**
	 	 * 给“搜索”按钮增加监听事件
	 	 * add ActionListener for button<t>Search<\t>  directly
	 	 */
		search.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent evt) {
	
				countFiles = 0;
				result.setText("");
	
				f = textFilter.getText().trim();
				p = textDirectory.getText().trim();
				
				if("".equals(f)) {
					System.out.println("filter can not be null,plese fill it");
				}if("".equals(p)) {
					System.out.println("path can not be null,please fill it!");
				}else {
					Thread searchT = new Thread(new Runnable(){

						public void run() {
							doSearch(f,p);
						}
					});
					searchT.start();
				}
			}
			
		});
		
	   
	    setLayout(new GridBagLayout());
	    GridBagConstraints gc = new GridBagConstraints(); 

	    gc.anchor = GridBagConstraints.WEST;
	    addComponet(gc, filter, 0, 0, 1, 1);
	    addComponet(gc, directory, 0, 1, 1, 1); 
   
	    gc.fill = GridBagConstraints.HORIZONTAL;
	    gc.weightx = 100;
	    addComponet(gc, textFilter, 1, 0, 1, 1);
	    addComponet(gc, textDirectory, 1, 1, 1, 1); 

	    gc.fill = GridBagConstraints.NONE;
	    gc.weightx = 0;
	    addComponet(gc, search, 2, 0, 1, 1);
	    addComponet(gc, browse, 2, 1, 1, 1); 
   
	    gc.fill = GridBagConstraints.HORIZONTAL;
	    gc.weightx = 100;
	    addComponet(gc, statusShow1, 0, 2, 3, 1); 
	    addComponet(gc, statusShow2, 0, 3, 3, 1); 
	    
	    gc.fill = GridBagConstraints.BOTH;
	    gc.weighty = 100;
	    addComponet(gc, js, 0, 4, 3, 1);
	}
	 public void addComponet(GridBagConstraints gc, Component c, int x, int y,
	   int w, int h)
	 {
	  gc.gridx = x;
	  gc.gridy = y;
	  gc.gridwidth = w;
	  gc.gridheight = h;
	  add(c, gc);
	 } 
	/**
	 * 搜索监听事件的实现
	 * implement the ActionLitener
	 */
	private void doSearch(String filter, String path) {
		File file = new File(path);
		if(file.exists()) {
			if(file.isDirectory()) {
				File[] fileArray = file.listFiles();
				for(File f:fileArray) {
					if(f.isDirectory()) {
						doSearch(filter,f.getPath());
					} else {
						if(f.getName().indexOf(filter) >= 0) {
							countFiles++;
							result.append(f.getPath() + "\r\n");
						}
					}
					statusShow1.setText(f.getPath());
				}
				statusShow2.setText("The numbers of files had been found:" + countFiles);
			} else {
				System.out.println("Couldn't open the path!");
			}
		} else {
			System.out.println("The path had been apointed was not Exist!");
		}
	}
}